#ifndef __COMPONENT_SWIM_AWAY_BEHAVIOR_H__
#define __COMPONENT_SWIM_AWAY_BEHAVIOR_H__

#include "ComponentBehavior.hpp"
#include "Engine/MathUtils.hpp"
#include "Engine/Vector2.hpp"

#include "EasyMachine.hpp"
#include "INoiseListener.hpp"

using namespace PocketSurvival::Engine;

namespace PocketSurvival
{
    struct ComponentCreature;
    struct ComponentBody;
    struct ComponentPathfinding;
    struct ComponentHealth;

    class SubsystemTerrain;

    struct ComponentSwimAwayBehavior : public ComponentBehavior
    {
    protected:
        enum class MachineState
        {
            Inactive,
            SwimmingAway            // 逃跑
        };

        class StateMachine : public EasyMachine<MachineState>
        {
        protected:
            ComponentSwimAwayBehavior * m_swimAway;
        public:
            StateMachine(ComponentSwimAwayBehavior *swimAway);

            virtual void enter() override;
            virtual void update(float dt) override;
            virtual void leave() override;
        };

    protected:
        float m_timeToForgetAttacker;
        float m_importanceLevel = 0.0f;
        StateMachine *m_stateMachine;

    public:
        ComponentCreature *componentCreature;
        ComponentPathfinding *componentPathfinding;
        ComponentBody *componentBody;                   // 自身body
        ComponentHealth *componentHealth;

        SubsystemTerrain *subsystemTerrain;

        entt::entity attacker = entt::null;             // 攻击者

        ComponentSwimAwayBehavior() = default;
        ~ComponentSwimAwayBehavior();

        virtual float getImportanceLevel() override;

        void swimAwayFrom(entt::entity attacker);
        Vector3 findSafePlace();
        float scoreSafePlace(const Vector3 &currentPosition, const Vector3 &safePosition, std::shared_ptr<Vector3> herdPosition);


        virtual void update(float dt) override;
        virtual void load(const nlohmann::json &jsonData) override;
        virtual const std::string &getName() const override;
        static const std::string &GetName();
    };
}

#endif
